<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="快递追踪号" prop="trackingNumber">
              <el-input
                v-model="queryParams.trackingNumber"
                placeholder="请输入快递追踪号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="下单日期">
        <el-date-picker
          v-model="daterangeOrderDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="收货方式" prop="shippingMethod">
        <el-select v-model="queryParams.shippingMethod" placeholder="请选择收货方式" clearable>
          <el-option
            v-for="dict in dict.type.shipping_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['mall:orders:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['mall:orders:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['mall:orders:remove']"
              >删除</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:orders:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="id"/>
      <el-table-column label="订单总金额(元)" align="center" prop="amount"/>
      <el-table-column label="支付金额(元)" align="center" prop="payAmount"/>
      <el-table-column label="优惠金额(元)" align="center" prop="promotionAmount"/>
      <el-table-column label="运费(元)" align="center" prop="freightAmount"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="快递追踪号" align="center" prop="trackingNumber"/>
      <el-table-column label="下单日期" align="center" prop="orderDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货方式" align="center" prop="shippingMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shipping_method" :value="scope.row.shippingMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="取货地点id" align="center" prop="deptId"/>
      <el-table-column label="收货人" align="center" prop="receiverName"/>
      <el-table-column label="收货人电话" align="center" prop="receiverPhone"/>
      <el-table-column label="收货人省" align="center" prop="receiverProvince" :formatter="provinceFormatter"/>
      <el-table-column label="收货人市" align="center" prop="receiverCity" :formatter="cityFormatter"/>
      <el-table-column label="收货人区" align="center" prop="receiverRegion" :formatter="regionFormatter"/>
      <el-table-column label="收货地址" align="center" prop="receiverAddress"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
            v-hasPermi="['mall:orders:edit']"
          >订单详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:orders:edit']"
          >修改
          </el-button>
          <!--  <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['mall:orders:remove']"
            >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单总金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入订单总金额"/>
        </el-form-item>
        <el-form-item label="运费" prop="freightAmount">
          <el-input v-model="form.freightAmount" placeholder="请输入运费"/>
        </el-form-item>
        <el-form-item label="优惠金额" prop="promotionAmount">
          <el-input v-model="form.promotionAmount" placeholder="请输入优惠金额"/>
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入支付金额"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递追踪号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入快递追踪号"/>
        </el-form-item>
        <!--        <el-form-item label="下单日期" prop="orderDate">
                  <el-date-picker clearable
                                  v-model="form.orderDate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="请选择下单日期">
                  </el-date-picker>
                </el-form-item>-->
        <el-form-item label="收货方式" prop="shippingMethod">
          <el-select v-model="form.shippingMethod" placeholder="请选择收货方式">
            <el-option
              v-for="dict in dict.type.shipping_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="取货地点" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入取货地点id"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <orderDetail :isOpen.sync="openDetail" :orderId="detailOrderId"/>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from '@/api/mall/orders'
import { codeToText } from 'element-china-area-data'
import orderDetail from '@/views/mall/orders/detail.vue'

export default {
  name: 'Orders',
  components: {orderDetail },
  dicts: ['order_status', 'delete_flag', 'shipping_method'],
  data() {
    return {
      openDetail: false,
      detailOrderId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 订单编号时间范围
      daterangeOrderDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        amount: null,
        freightAmount: null,
        promotionAmount: null,
        payAmount: null,
        status: null,
        trackingNumber: null,
        orderDate: null,
        shippingMethod: null,
        deptId: null,
        receiverName: null,
        receiverPhone: null,
        receiverProvince: null,
        receiverCity: null,
        receiverRegion: null,
        receiverAddress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: '用户id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    regionFormatter(row) {
      return codeToText[row.receiverRegion]
    },
    provinceFormatter(row) {
      return codeToText[row.receiverProvince]
    },
    cityFormatter(row) {
      return codeToText[row.receiverCity]
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeOrderDate && '' != this.daterangeOrderDate) {
        this.queryParams.params['beginOrderDate'] = this.daterangeOrderDate[0]
        this.queryParams.params['endOrderDate'] = this.daterangeOrderDate[1]
      }
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        amount: null,
        freightAmount: null,
        promotionAmount: null,
        payAmount: null,
        status: null,
        trackingNumber: null,
        orderDate: null,
        shippingMethod: null,
        deptId: null,
        receiverName: null,
        receiverPhone: null,
        receiverProvince: null,
        receiverCity: null,
        receiverRegion: null,
        receiverAddress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleDetail(row) {
      this.openDetail = true
      this.detailOrderId=row.id
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改订单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrders(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mall/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
